/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package processer;

import main.BotManager;
import main.Constants;
import main.IGameDisplay;
import utils.Debug;

/**
 *
 * @author Administrator
 */
public class Waiter extends Processer implements IGameDisplay {

    private int playerId;
    private int numberPlayers;
    private int tableHostId;
    private int numberPlayerReadys;
    private int maxPlayer;
    private int myViewId;
    private long startTime;
    private int timeCount;
    private String moneyBet;
    private int gameRule;
    private int gamePlayType;
    private boolean canReady;
    private String[] listChat = {"san sang", "ss", "he he", "hi hi", "choi di", "cho gi nua"};
    private int[] listMoneyBet1 = {0, 8000, 5000, 10000, 2000, 50000};
//    private int[] listMoneyBet2 = {15000, 17000, 20000, 18500, 25000, 30000};
    private int[] listMoneyBet3 = {60000, 40000, 25000, 30000, 250000, 100000};
    private int betIndex;
    private boolean isWaitingMoney;
    private int moneyBetNeeded;
    private boolean isChat;
    private boolean isBetMoney;

    public Waiter(RoBot parent, GameData gameData, BotManager manager) {
        super(parent, gameData, manager);
        initValue();
    }

    private void initValue() {
        gameData.setGameDisplay(this);
        this.maxPlayer = gameData.getMaxPlayer();
        this.numberPlayerReadys = gameData.getNumberPlayerReadys();
        this.numberPlayers = gameData.getNumberPlayers();
        this.tableHostId = gameData.getTableHostId();
        this.playerId = gameData.getPlayerId();
        this.myViewId = gameData.getMyViewId();
        gameData.myMoney = gameData.playerMoneys[playerId];
        startTime = System.currentTimeMillis();
        if (playerId != tableHostId) {
            readyPlay();
        } else {
            if (gameData.numberMatchPlays > 30) {
                gameData.numberMatchPlays = 0;
                parent.request.exitTable();
            }
        }

    }

    private void betMoney() {
        moneyBetNeeded = 0;
        betIndex = utils.Utils.nextInt(6);
        if (gameData.roomId >= 0 && gameData.roomId <= 4) {
            moneyBetNeeded = listMoneyBet1[betIndex];
        } else if (gameData.roomId >= 5 && gameData.roomId <= 7) {
            moneyBetNeeded = listMoneyBet1[betIndex];
        } else if (gameData.roomId >= 8 && gameData.roomId <= 9) {
            moneyBetNeeded = listMoneyBet3[betIndex];
        }
        if (gameData.myMoney <= moneyBetNeeded * 5) {
            parent.request.exitTable();
        }
        isBetMoney = true;
        parent.request.betMoney(String.valueOf(moneyBetNeeded));
    }

    private void readyPlay() {
        Debug.d("money bet :" + gameData.getMoneyBet());
        Debug.d(gameData.myName + ",money:" + gameData.myMoney);
        int moneyNeeded = 5 * Integer.parseInt(gameData.getMoneyBet());
        if (gameData.myMoney > moneyNeeded) {
//            gameData.readyStates[playerId] = 1;
            parent.request.changeReadyState(1);
        }
//        } else {
//            parent.request.setMoney(moneyNeeded + 100);
//            
//            isWaitingMoney = true;
//        }
    }
//----------------------------------------------------------------------------

    private void checkMoney() {
        Debug.d("money bet :" + gameData.getMoneyBet());
        Debug.d(gameData.myName + ",money:" + gameData.myMoney);
        int moneyNeeded = 5 * Integer.parseInt(gameData.getMoneyBet());
        if (gameData.myMoney < moneyNeeded) {
            parent.request.setMoney(moneyNeeded + 100);
            parent.request.exitTable();
        }
    }

    @Override
    public void doLoop() {
        if (System.currentTimeMillis() - startTime > 5000) {
            startTime = System.currentTimeMillis();
            canReady = true;
        }
        checkMoney();
        if (canReady) {
            canReady = false;
            Debug.d("time count:" + timeCount);
            Debug.d("number player :" + numberPlayers);
            Debug.d("number ready :" + numberPlayerReadys);
            if (playerId == tableHostId && gameData.readyStates[playerId] == 1) {
                parent.request.changeReadyState(0);
                return;
            }
            if (numberPlayers > 1 && gameData.readyStates[playerId] == 0 && playerId != tableHostId) {
                readyPlay();
                return;
            }
            if (playerId == tableHostId) {
                if ((gameData.numberMatchPlays % 3 == 0 || gameData.numberMatchPlays == 0) && !isBetMoney) {
                    betMoney();
                }
            }

            if (playerId == tableHostId && numberPlayerReadys == numberPlayers - 1 && numberPlayers != 1) {
                {
                    if ((gameData.numberMatchPlays % 3 == 0 || gameData.numberMatchPlays == 0) && !isBetMoney) {
                        betMoney();
                    } else {
                        isBetMoney = false;
                        parent.request.changeReadyState(1);
                    }
                }
                return;
            }
        }
    }
//--------------------------------------------------------------------------

    @Override
    public void processMesage(String[] part) {
        Debug.d("process waiter :" + part[0]);
        super.processMesage(part);
        if (part[0].equals("USE")) {
            gameData.setNumberPlayerInTable(part);
            return;
        }
        if (part[0].equals("SETMONEY")) {
            gameData.myMoney = Integer.parseInt(part[1]);
            if (isWaitingMoney) {
                parent.request.exitTable();
            }
            return;
        }
        if (part[0].equals("SE")) {
            gameData.setNumberPlayerInTable(part);
            gameData.diposeGameDisplay();
            parent.changeProcesser(RoBot.TABLE_CHOOSER);
            return;
        }
        if (part[0].equals("SM")) {
            gameData.setTableMoneyBet(part);
            return;
        }

        if (part[0].equals("RO")) {
            gameData.setRoArrData(part);
            return;
        }
        if (part[0].equals("MO")) {
            gameData.setMoArrData(part);
            gameData.myMoney = gameData.playerMoneys[playerId];
            checkMoney();
            return;
        }
        if (part[0].equals("RE")) {
            gameData.setReArrData(part);
            return;
        }
        if (part[0].equals("MS")) {
            moneyBet = part[1];
            gameData.setMoneyBet(part[1]);
            return;
        }
        if (part[0].equals("INFO")) {
            gameData.setInfoData(part);
            return;
        }
        if (part[0].equals("GM")) {
            this.gameRule = Integer.parseInt(part[1]);
            gameData.setGameRule(gameRule);
            return;
        }
        if (part[0].equals("CHAT")) {
        }
        if (part[0].equals("KICK")) {
            gameData.setKickOut(true);
            return;
        }
        if (part[0].equals("MF")) {
            return;
        }
        if (part[0].equals("RMF")) {
        }
        if (part[0].equals("SC")) {
            gameData.setCardInHandData(part);
            return;
        }
        if (part[0].equals("FS")) {
            gameData.setFirstTurn(Integer.parseInt(part[1]));
            gamePlayType = gameData.gameTypePlay;
            Debug.d("game type :" + gamePlayType);
            if (gamePlayType == Constants.TIEN_LEN) {
                parent.changeProcesser(RoBot.PLAYING_TIEN_LEN);
            } else if (gamePlayType == Constants.PHOM) {
                parent.changeProcesser(RoBot.PLAYING_PHOM);
            }
            return;
        }
        if (part[0].equals("START")) {
            gameData.setFirstTurn(Integer.parseInt(part[1]));
            return;
        }
        if (part[0].equals("S3C")) {
            parent.changeProcesser(RoBot.PLAYING_BA_CAY);
            return;
        }
        if (part[0].equals("RS3")) {
//            gameData.setBaCayData(part);
            return;
        }
        if (part[0].equals("UPASS")) {
            gameData.setTablePass(part);
            return;
        }

    }

    @Override
    public void setPlayerId(int id) {
        this.playerId = id;
    }

    @Override
    public void setNumberPlayer(int number) {
        this.numberPlayers = number;
    }

    @Override
    public void setTableHostId(int id) {
        this.tableHostId = id;
    }

    @Override
    public void setNumberPlayerReady(int number) {
        this.numberPlayerReadys = number;
    }

    @Override
    public void setMaxPlayer(int number) {
        this.maxPlayer = number;
    }

    @Override
    public void setGameRule(int gameRule) {
    }

    @Override
    public void setMyViewId(int id) {
        this.myViewId = id;
    }
}
